package com.boarsoft.boar.message.biz.impl;

import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boarsoft.boar.message.biz.TargetQueueBiz;
import com.boarsoft.boar.message.entity.TargetQueueEntity;
import com.boarsoft.common.Util;
import com.boarsoft.common.dao.PagedResult;
import com.boarsoft.hibernate.biz.SimpleBizImpl;

@Component
public class TargetQueueBizImpl extends SimpleBizImpl implements TargetQueueBiz {

	@Override
	@Transactional(readOnly = true)
	public PagedResult<TargetQueueEntity> list(String catalog, String key, String orderBy, int pageNo, int pageSize) {
		StringBuilder sb = new StringBuilder();
		if (Util.strIsNotEmpty(catalog)) {
			sb.append(" and catalog='").append(catalog).append("'");
		}
		if (Util.strIsNotEmpty(key)) {
			sb.append(" and (code like '%").append(key);
			sb.append("%' or memo like '%").append(key);
			sb.append("%' or name like '%").append(key);
			sb.append("%')");
		}
		int total = dao.getTotal(TargetQueueEntity.class, sb.toString());
		//
		if (Util.strIsNotEmpty(orderBy)) {
			sb.append(" order by ").append(orderBy);
		} else {
			sb.append(" order by code");
		}
		List<TargetQueueEntity> lt = dao.list(TargetQueueEntity.class, sb.toString(), pageNo, pageSize);
		return new PagedResult<TargetQueueEntity>(total, lt, pageNo, pageSize);
	}

	@Override
	@Transactional
	public void save(TargetQueueEntity o) {
		if (Util.strIsEmpty(o.getId())) {
			dao.save(o);
		} else {
			dao.update(o);
		}
	}

	@Override
	@Transactional
	public TargetQueueEntity delete(String id) {
		TargetQueueEntity o = dao.get(TargetQueueEntity.class, id);
		dao.delete(o);
		return o;
	}

	@Override
	@Transactional(readOnly = true)
	public TargetQueueEntity get(String id) {
		return (TargetQueueEntity) dao.get(TargetQueueEntity.class, id);
	}

	@Override
	public List<TargetQueueEntity> list() {
		return dao.list(TargetQueueEntity.class);
	}
}